package com.hq.hotl.controller;

import com.hq.hotl.dao.CustomerDao;
import com.hq.hotl.pojo.Customer;

import com.hq.hotl.pojo.House;

import com.hq.hotl.pojo.PageData;
import com.hq.hotl.service.CutomerService;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


import java.util.List;
import java.util.Map;

@Controller
public class CustomController {
    @Autowired
    private CutomerService cutomerService;

    @Autowired
    private CustomerDao customerDao;

    //customer登录

    @RequestMapping("customerLogin")
    public String customerLogin(String username, String password ,ModelMap map) {
        System.out.println(username + password);
        //先判断入参
        if (username == null || password == null) {
            //入参为空
            return "zhuceCustomer";
        }
        //调用service层来获取到当前用户名的全部用户信息
        Map<String, Object> login = cutomerService.login(username, password);
        if (login == null) {
            return "zhuceCustomer";
        }
        System.out.println(login.values());
            Object customer = login.get("customer");
            map.put("customer",customer);
            return "redirect:/index.jsp";
    }

    @RequestMapping("/login")
    public String login(){
        return "/login.jsp";
    }

    public CutomerService getCutomerService() {
        return cutomerService;
    }

    @RequestMapping(path = "/type", method = {RequestMethod.GET})
    public String orderHouse(String type, ModelMap modelMap) {
        List<House> resultmap = cutomerService.orderHouse(type);
        modelMap.put("result", resultmap);
        if ("单人间".equals(type)) {
            return "orderSingle";
        } else {
            return "orderDouble";
        }
    }

    @RequestMapping(path = "/order")
    public String orderReturn(String name, ModelMap modelMap) {
        int i = cutomerService.updateOne(name);
        modelMap.put("name",name);
        System.out.println(name);
        return "orderFinal";
    }

    @RequestMapping(path="/insertOne")
    public String insertOne(int customer_id,int order_customer_id,double price){
        int i = cutomerService.insertOne(customer_id,order_customer_id, price);
       /* modelMap.put("customer_id",customer_id);
        modelMap.put("order_customer_id",order_customer_id);
        modelMap.put("price",price);
        System.out.println(customer_id+order_customer_id+price);
        System.out.println(i);*/
        return "victory";
    }

    //注册customer
    @RequestMapping("zhuce")
    public String zhuce(Customer customer){
        //调用service层的方法来将数据添加用户
        int zhuce = cutomerService.zhuce(customer);
        System.out.println(customer);
        //判断是否添加成功
        if (zhuce==0){
            //添加失败
            return "zhuce";
        }else {
            return "zhuce-LoginCustomer";
        }
    }
    //分页查询全部的customer信息
    @RequestMapping("/list5")
    public String getAllByPage(Integer currentPage,Integer pageSize,ModelMap modelMap){
        //第一次页面显示，可以不用穿参数，使用Integer来获取一个null值的参数，自行判断
        if (currentPage==null){
            currentPage=1;
        }
        if (pageSize==null){
            pageSize=5;
        }
        //直接调勇业务层实现业务，获取到模型数据
        PageData pageData=cutomerService.getAllByPage(currentPage,pageSize);
        //跳转jsp页面，同时传递这些数据，使用ModelMap来共享数据，给jsp传递上下文数据
        modelMap.put("pageData",pageData);
        System.out.println(pageData.getPageSize());
        //跳转jsp页面
        return "list5";
    }
    //删除一条customer信息
    @RequestMapping("deleteccOne")
    public String deleteOne(int id){
        Boolean bool = cutomerService.deleteOne(id);
        return "list5";

    }

    //获取到传入的id的全部信息
    @RequestMapping("queryccOne")
    public String queryOne(int id,ModelMap modelMap){
        Customer customer = cutomerService.queryOneCustomer(id);
        modelMap.put("customer",customer);
        return "updateCustomer";
    }

    //修改customer信息
    @RequestMapping("updateCustomer")
    public String updateCustomer(Customer customer,Integer currentPage,Integer pageSize){
       //判断入参书否为空
        if (customer==null){
            return "updateCustomer";
        }
        Boolean bool = cutomerService.updateOneCustomer(customer);
        //判断是否修改成功
        if (!bool){
            //没有修改成功
            return "updateCustomer";
        }
        else {
            return "redirect:/list5?currentPage="+1+"&pageSize="+5;
        }


    }
    //
    @RequestMapping("centerupdateCustomer")
    public String centerupdateCustomer(Customer customer){
        //判断入参书否为空
        if (customer==null){
            return "changepassword";
        }
        Boolean bool = cutomerService.updateOneCustomer(customer);
        //判断是否修改成功
        if (!bool){
            //没有修改成功
            return "changepassword";
        }
        return "profile";
    }

    //顾客个人信息查询
    @RequestMapping("queryCustomerOne")
    public String queryOne(String username ,ModelMap modelMap){
        Customer customer = customerDao.queryOne(username);
        modelMap.put("customer",customer);
        return "profile";
    }

    @RequestMapping("/updatePassword")
    public String updatePassword(Customer customer) {
        int i = cutomerService.updatePassword(customer);
        if (i == 0){
            return "changepassword";
        }else{
            return "profile";
        }
    }

    @RequestMapping("/center")
    public String personalcenter(String username,ModelMap modelMap){
        Customer customer = cutomerService.queryOne(username);
        modelMap.put("customer",customer);
        return "/personalcenter";
    }

    @RequestMapping("funcation3")
    public String funcation3(String username , ModelMap modelMap){
        Customer customer = customerDao.queryOne(username);
        modelMap.put("customer",customer);
        return "changepassword";
    }
}

